import Taro ,{ Component } from '@tarojs/taro'
import { View  , Text ,Ad ,Image} from '@tarojs/components'

import './newsDetail.scss'
import BasePage from "../basePage";

import {base_url} from "../../network/config";
import DataManager from "../../utils/DataManager";

import WxParse from '../../component/wxParse/wxParse'

export default class newsDetail extends BasePage{

  config = {
    navigationBarTitleText: '新闻详情'
  }

  constructor() {
    super(...arguments)
    this.state={
      newsData:{},
      showAd:true,
    }
  }

  componentWillMount(){
    this.newsId = this.$router.params.newsId;
    console.log('newsId---------' + this.newsId)
  }

  componentDidMount(){
    this.loadNewsDetail();
  }

  onAdError = (e) =>{
    if (e){
      this.setState({
        showAd:false
      })
    }
  }

  onAdLoad = () => {
    this.setState({
      showAd:true
    })
  }

  loadNewsDetail(){
    let _this = this;

    let url = base_url + 'stateless/news/newsDetail'
    Taro.request({
      url:url,
      data: {
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'c',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        newsId:_this.newsId
      },
      method: 'GET',
      success(res) {
        _this.setState({
          newsData:res.data.data,
        })
        const article = res.data.data.content
        WxParse.wxParse('article', 'html', article, _this.$scope, 5)
      }
    })
  }

  onBackToHome = () =>{
    Taro.reLaunch({
      url:`/pages/index/index`
    })
  }


  render(){

    let adStyle = this.state.showAd ? 'ad-style' : 'ad-invisible';

    return(
      <View className='news-detail-container'>
        <View className='news-detail-sub'>
          <Text className='news-detail-title'>{this.state.newsData.title}</Text>
          <View className='news-detail-content'>
            <import src='../../component/wxParse/wxParse.wxml' />
            <template is='wxParse' data='{{wxParseData:article.nodes}}'/>
          </View>
        </View>
        <View className={adStyle}>
          <Ad unitId='adunit-c5ccf5077e093a65' onError={this.onAdError.bind(this)} onLoad={this.onAdLoad.bind(this)}></Ad>
        </View>

        <View className='shop-detail-share'  onClick={this.onBackToHome.bind(this)}>
          <Text className='shop-detail-share-text'>回到</Text>
          <Text className='shop-detail-share-text'>首页</Text>
        </View>
      </View>
    )
  }


}




